package com.ge.modules.api.service.impl;

import com.ge.modules.api.service.TbOrderHeaderService;
import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.dao.TbOrderDao;
import com.ge.modules.dao.TbOrderHeaderDao;
import com.ge.modules.dao.TbUserCardDao;
import com.ge.modules.dao.TbUserDao;
import com.ge.modules.entity.TbOrderEntity;
import com.ge.modules.entity.TbOrderHeaderEntity;
import com.ge.modules.entity.TbUser;
import com.ge.modules.entity.TbUserCardEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class TbOrderHeaderServiceImpl implements TbOrderHeaderService {
    @Autowired
    private TbOrderHeaderDao orderHeaderDao;

    @Autowired
    private TbOrderDao tbOrderDao;

    @Autowired
    private TbUserCardDao userCardDao;

    @Autowired
    private TbUserDao userDao;

    @Override
    public TbOrderHeaderEntity queryObject(Integer id) {
        return orderHeaderDao.queryObject(id);
    }

    @Override
    public TbOrderHeaderEntity queryObject(TbOrderHeaderEntity tbOrderHeaderEntity) {
        return orderHeaderDao.queryObject(tbOrderHeaderEntity);
    }

    @Override
    public List<TbOrderHeaderEntity> queryList(Map<String, Object> map) {
        return orderHeaderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return orderHeaderDao.queryTotal(map);
    }

    @Override
    public void save(TbOrderHeaderEntity orderHeader) {
        orderHeaderDao.save(orderHeader);
    }

    @Override
    public void update(TbOrderHeaderEntity orderHeader) {
        orderHeaderDao.update(orderHeader);
    }

    @Override
    public void delete(Integer id) {
        orderHeaderDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        orderHeaderDao.deleteBatch(ids);
    }

    @Override
    public void updateOrderPayStatus(TbOrderHeaderEntity tbOrderHeaderEntity) {
        orderHeaderDao.update(tbOrderHeaderEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("orderHeaderCode", tbOrderHeaderEntity.getOrderheaderCode());
        List<TbOrderEntity> tbOrderEntityList = tbOrderDao.queryList(map);
        //循环修改订单状态和支付类型
        for (int i = 0; i < tbOrderEntityList.size(); i++) {
            TbOrderEntity tbOrderEntity = tbOrderEntityList.get(i);
            //修改为待接单状态
            tbOrderEntity.setOrderStatus(1);
            tbOrderEntity.setOrderpayType(tbOrderHeaderEntity.getOrderPayType());
            tbOrderDao.update(tbOrderEntity);
//			修改用户积分
//        TbUser tbUser = userDao.selectByPrimaryKey(tbOrderEntity.getUserId());
//        tbUser.setIntegral(tbUser.getIntegral() + tbOrderEntity.getOrderPrice());
//        userDao.update(tbUser);
        }
    }

    @Override
    public void updateOrderAndUserCard(TbOrderHeaderEntity tbOrderHeaderEntity, TbUserCardEntity userCard) {
        orderHeaderDao.update(tbOrderHeaderEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("orderHeaderCode", tbOrderHeaderEntity.getOrderheaderCode());
        List<TbOrderEntity> tbOrderEntityList = tbOrderDao.queryList(map);
        //循环修改订单状态和支付类型
        for (int i = 0; i < tbOrderEntityList.size(); i++) {
            TbOrderEntity tbOrderEntity = tbOrderEntityList.get(i);
            //修改为待接单状态
            tbOrderEntity.setOrderStatus(1);
            tbOrderEntity.setOrderpayType(tbOrderHeaderEntity.getOrderPayType());
            tbOrderDao.update(tbOrderEntity);
            //修改用户积分
//        TbUser tbUser = userDao.selectByPrimaryKey(tbOrderEntity.getUserId());
//        tbUser.setIntegral(tbUser.getIntegral() + tbOrderEntity.getOrderPrice());
//        userDao.update(tbUser);
        }
        userCardDao.update(userCard);
    }
}
